package com.itheima.zxingdemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

public class MainAbilitySlice extends AbilitySlice {

    private DependentLayout myLayout = new DependentLayout(this);
    private Context mContext= null;
    private String cameraPermission = SystemPermission.CAMERA;
    private String[] permission = new String[]{SystemPermission.CAMERA,SystemPermission.WRITE_USER_STORAGE,SystemPermission.READ_USER_STORAGE};
    private static int CAMERAPERMISSION = 10;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);
        mContext = this;

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("点击跳转功能页面");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(component -> {
            if (isCameraAllow()){
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("com.itheima.zxingdemo")
                        .withAbilityName("com.itheima.zxingdemo.MainDemoAbility")
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }else{
                checkCameraPermission();
            }
        });

        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    //检查权限
    private void checkCameraPermission() {

        if (!isCameraAllow()) {
            // 调用者无权限，做错误处理
            new ToastDialog(mContext).setContentText("no Camera permission").show();
            if (!canRequestPermission(cameraPermission)){
                new ToastDialog(mContext).setContentText("can not request Camera permission").show();
            }else{
                requestPermissionsFromUser(permission,CAMERAPERMISSION);
            }
            return;
        }
    }

    //判断是否已经获取相机权限
    private boolean isCameraAllow(){
        return verifySelfPermission(cameraPermission) == IBundleManager.PERMISSION_GRANTED;
    }

}
